set more 1
use ..\data\paper_data.dta, clear
drop if treatment_id==3
xi i.treatment_id*i.part, noomit
global reps=9999
global seed=96941726 // random number
global treatvars _Itreatment_1 _Itreatment_2

preserve

* Section C
*==========

*according to exact Wilcoxon-Mann-Whitney tests, treatment differences in mistakes' distributions are statistically insignificant ($p\geq0.143$). 

keep if period==1
bysort team_id (subject_id period): gen rank=_n
gen mistakes=mistakes_pt1+mistakes_pt2+mistakes_pt3
ranksum mistakes if rank==1 & treatment_id!=2, by(treatment_id)
ranksum mistakes if rank==1 & treatment_id!=1, by(treatment_id)
ranksum mistakes if rank==1 & treatment_id!=0, by(treatment_id)

restore
preserve

* Section D
*==========

gen disagreement_effort_any=(g_own_1!=g_partner_1)
gen disagreement_effort_unsolved=(disagreement_effort_any==1 & g_own_2!=g_partner_2)
gen disagreement_offer=(amount_own_1!=amount_partner_1) | (request_own_1!=request_partner_1) | (sign_own_1!=sign_partner_1) | (target_own_1!=target_partner_1)
gen disagreement_offer_unsolved=((amount_own_1!=amount_partner_1) & (amount_own_2!=amount_partner_2)) | ((request_own_1!=request_partner_1) & (request_own_2!=request_partner_2)) | ((sign_own_1!=sign_partner_1) & (sign_own_2!=sign_partner_2)) | ((target_own_1!=target_partner_1) & (target_own_2!=target_partner_2))
order disagreement*, after(g_own_2)

bysort economy_id period: egen disagreement_effort_unsolved_max=max(disagreement_effort_unsolved)
bysort economy_id period: egen disagreement_offer_unsolvedmax=max(disagreement_offer_unsolved)

collapse disagreement* gstar earnings_tot $treatvars, by(treatment_id economy_id part period)

* A first observation is that effort disagreements tend to be significantly more frequent in Decentralized and Structured than in Baseline in Part 3 ($p\leq0.018$), but not so in Parts 1 and 2 ($p\geq0.231$).
reg disagreement_effort_unsolved $treatvars if part==1, vce(cluster economy_id)
boottest {_Itreatment_1}{_Itreatment_2}{_Itreatment_2=_Itreatment_1}, boottype(wild) weighttype(webb) reps($reps) seed($seed) noci nograph cluster(economy_id)
reg disagreement_effort_unsolved $treatvars if part==2, vce(cluster economy_id)
boottest {_Itreatment_1}{_Itreatment_2}{_Itreatment_2=_Itreatment_1}, boottype(wild) weighttype(webb) reps($reps) seed($seed) noci nograph cluster(economy_id)
reg disagreement_effort_unsolved $treatvars if part==3, vce(cluster economy_id)
boottest {_Itreatment_1}{_Itreatment_2}{_Itreatment_2=_Itreatment_1}, boottype(wild) weighttype(webb) reps($reps) seed($seed) noci nograph cluster(economy_id)

* A second observation is the absence of a clear trend across periods in effort disagreements in all treatments in Parts 2 and 3 ($p\geq0.366$). In Part 1, a significant negative trend is estimated for Decentralized and Structured ($p\leq0.064$) but not for Baseline ($p=0.856$). 
forval t=0/2{
	forval p=1/3{
		di "treatment_id==`t' & part==`p'"
		reg disagreement_effort_unsolved c.period if treatment_id==`t' & part==`p', vce(cluster economy_id)
		boottest {period}, boottype(wild) weighttype(webb) reps($reps) seed($seed) noci nograph cluster(economy_id)
	}
}

* A third observation is that disagreements in offers are more prevalent in Decentralized than in Structured in both Parts 2 and 3 ($p\leq0.02$).
reg disagreement_offer_unsolved _Itreatment_2 if part==2 & treatment_id!=0, vce(cluster economy_id)
boottest {_Itreatment_2}, boottype(wild) weighttype(webb) reps($reps) seed($seed) noci nograph cluster(economy_id)
reg disagreement_offer_unsolved _Itreatment_2 if part==3 & treatment_id!=0, vce(cluster economy_id)
boottest {_Itreatment_2}, boottype(wild) weighttype(webb) reps($reps) seed($seed) noci nograph cluster(economy_id)

* Finally, we do not detect a significant correlation between disagreements and total earnings ($p\geq0.208$).
reg earnings_tot disagreement_effort_unsolved i.part i.treatment_id, vce(cluster economy_id)
boottest {disagreement_effort_unsolved}, boottype(wild) weighttype(webb) reps($reps) seed($seed) noci nograph cluster(economy_id)
reg earnings_tot disagreement_effort_unsolved disagreement_offer_unsolved i.part i.treatment_id if treatment_id>0, vce(cluster economy_id)
boottest {disagreement_effort_unsolved} {disagreement_offer_unsolved}, boottype(wild) weighttype(webb) reps($reps) seed($seed) noci nograph cluster(economy_id)

restore
preserve

* Section G
*==========

gen treatment_id2=treatment_id
replace treatment_id2=3 if date=="180618_0927" | (date=="180618_1428" & economy_id==61) | date=="180620_0947"
xi i.treatment_id2, noomit
global treatvars2 _Itreatment_2 _Itreatment_3 _Itreatment_1

collapse high_gstar gstar target_team offer_made offer_taken _Itreatment_* if treatment_id2>1, by(treatment_id treatment_id2 economy_id team_id part period)

gen offer_made_ip10=offer_made if treatment_id==2
replace offer_made_ip10=offer_made if treatment_id==1 & part==2 & high_gstar==0
replace offer_made_ip10=offer_made if treatment_id==1 & part==3 & high_gstar==0 & target_team>=5 & target_team<.

gen offer_taken_ip10=offer_taken if treatment_id==2
replace offer_taken_ip10=offer_taken if treatment_id==1 & part==2 & high_gstar==0
replace offer_taken_ip10=offer_taken if treatment_id==1 & part==3 & target_team>=5 & target_team<.

bysort economy_id period: egen n_offers_made_ip10=sum(offer_made_ip10)
bysort economy_id period: egen n_offers_fulfilled_ip10=sum(offer_taken_ip10)

collapse (max)offer_made offer_taken n_* _Itreatment_*, by(treatment_id treatment_id2 economy_id part period)
replace offer_taken=0 if offer_taken==.
replace n_offers_fulfilled_ip10=0 if n_offers_fulfilled_ip10==.

reg offer_made _Itreatment_3, cluster(economy_id)
boottest {_Itreatment_3}, boottype(wild) weighttype(webb) reps($reps) seed($seed) noci nograph cluster(economy_id)
reg offer_taken _Itreatment_3, cluster(economy_id)
boottest {_Itreatment_3}, boottype(wild) weighttype(webb) reps($reps) seed($seed) noci nograph cluster(economy_id)
reg n_offers_made_ip10 _Itreatment_3, cluster(economy_id)
boottest {_Itreatment_3}, boottype(wild) weighttype(webb) reps($reps) seed($seed) noci nograph cluster(economy_id)
reg n_offers_fulfilled_ip10 _Itreatment_3, cluster(economy_id)
boottest {_Itreatment_3}, boottype(wild) weighttype(webb) reps($reps) seed($seed) noci nograph cluster(economy_id)


* Section H
*==========

use ..\data\coders_output.dta, clear
global cat "confused_us confused_them coordination difficult prosocial antisocial overconfident giveuppromises disagree"
sort treatment_id team_id period effort_stage
egen stage_id=group(effort_stage)
reshape wide $cat rowtot, i(treatment_id team_id period stage_id) j(coder_id)
order confused_us* confused_them* coordination* difficult* prosocial* antisocial* overconfident* giveuppromises* disagree*, after(stage_id)
global cat "confused_us0 confused_them0 coordination0 difficult0 prosocial0 antisocial0 overconfident0 giveuppromises0 disagree0 confused_us1 confused_them1 coordination1 difficult1 prosocial1 antisocial1 overconfident1 giveuppromises1 disagree1"
foreach v of varlist $cat{
	replace `v'=0 if `v'==.
}
drop if rowtot0==. | rowtot1==.

*Coder 1 assigned 99.96% of the conversations to a single category. Coder 2, instead, assigned 88.32\% of the conversations to a single category, 11.12\% to two categories, and 0.56% to three or more categories.
tab rowtot0 effort_stage, col
tab rowtot1 effort_stage, col

*Overall, 66.75% of the conversations were assigned to the same category by the two coders.
gen confused_us_dif=abs(confused_us0-confused_us1)
gen confused_them_dif=abs(confused_them0-confused_them1)
gen coordination_dif=abs(coordination0-coordination1)
gen difficult_dif=abs(difficult0-difficult1)
gen prosocial_dif=abs(prosocial0-prosocial1)
gen antisocial_dif=abs(antisocial0-antisocial1)
gen overconfident_dif=abs(overconfident0-overconfident1)
gen giveuppromises_dif=abs(giveuppromises0-giveuppromises1)
gen disagree_dif=abs(disagree0-disagree1)
gen agree=0
replace agree=1 if (confused_us_dif+confused_them_dif+coordination_dif+difficult_dif+prosocial_dif+antisocial_dif+overconfident_dif+giveuppromises_dif+disagree_dif==0)
sum agree, det

*Furthermore, 75.27% of the conversations were assigned by both coders to at least one identical category.
gen disagree2=0
replace disagree2=1 if (rowtot0==1 & rowtot1==1) & (confused_us_dif+confused_them_dif+coordination_dif+difficult_dif+prosocial_dif+antisocial_dif+overconfident_dif+giveuppromises_dif+disagree_dif>0)
replace disagree2=1 if (rowtot0>1 | rowtot1>1) & (confused_us_dif+confused_them_dif+coordination_dif+difficult_dif+prosocial_dif+antisocial_dif+overconfident_dif+giveuppromises_dif+disagree_dif>1)
gen agree2=1-disagree2
sum agree2, det